package demoMod.invincibleOne.patches;

import com.evacipated.cardcrawl.modthespire.lib.*;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.characters.AnimatedNpc;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.events.AbstractEvent;
import com.megacrit.cardcrawl.events.GenericEventDialog;
import com.megacrit.cardcrawl.events.beyond.SpireHeart;
import com.megacrit.cardcrawl.helpers.MonsterHelper;
import com.megacrit.cardcrawl.localization.EventStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import com.megacrit.cardcrawl.vfx.cardManip.PurgeCardEffect;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.characters.InvincibleOne;
import sun.security.provider.ConfigFile;

import javax.print.attribute.standard.MediaTray;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import static com.megacrit.cardcrawl.events.AbstractEvent.logMetric;
import static com.megacrit.cardcrawl.helpers.MonsterHelper.THE_HEART_ENC;

public class SpireHeartPatch {
    private static int screen;
    private static final EventStrings eventStrings = CardCrawlGame.languagePack.getEventString(
            InvincibleOneMod.makeID("SpireHeart"));
    private static final String[] DESCRIPTIONS = eventStrings.DESCRIPTIONS;
    private static final String[] OPTIONS = eventStrings.OPTIONS;
    @SpirePatch(
            clz = SpireHeart.class,
            method = SpirePatch.CONSTRUCTOR)
    public static class DialogHeartPatch {
        @SpireInsertPatch(loc = 71)
        public static SpireReturn<Void> Postfix(SpireHeart _instance) {
            if (AbstractDungeon.player instanceof InvincibleOne) {
                screen = 1;
                try {
                    Field f = AbstractEvent.class.getDeclaredField("body");
                    f.setAccessible(true);
                    f.set(_instance, DESCRIPTIONS[0]);
                } catch (Exception ex) {
                    System.out.println("DialogHeartPatch ERROR: " + ex.getMessage());
                }
            }
            return SpireReturn.Continue();
        }
    }

    @SpirePatch(
            clz = SpireHeart.class,
            method = "buttonEffect",
            paramtypez = {int.class}
    )
    public static class FightHeartPatch {
        @SpirePrefixPatch()
        public static SpireReturn<Void> Prefix(SpireHeart _instance, int buttonPressed) {
            if (AbstractDungeon.player instanceof InvincibleOne) {
                switch (screen) {
                    case 1:
                        screen = 2;
                        _instance.roomEventText.updateBodyText(DESCRIPTIONS[1]);
                        _instance.roomEventText.updateDialogOption(0, OPTIONS[1]);
                        break;
                    case 2:
                        // 整补状态
                        List<AbstractCard> rm = new ArrayList<>();
                        for (AbstractCard c: AbstractDungeon.player.masterDeck.group) {
                            if (c.rarity != AbstractCard.CardRarity.BASIC) {
                                rm.add(c);
                            }
                        }
                        for (AbstractCard c: rm) {
                            AbstractDungeon.topLevelEffects.add(new PurgeCardEffect(c, (float) Settings.WIDTH / 2.0F, (float)Settings.HEIGHT / 2.0F));
                            AbstractDungeon.player.masterDeck.removeCard(c);
                        }
                        AbstractDungeon.player.currentHealth = AbstractDungeon.player.maxHealth;
                        AbstractDungeon.player.healthBarRevivedEvent();
                        AbstractDungeon.topPanel.panelHealEffect();
                        // 直接进入心脏战
                        (AbstractDungeon.getCurrRoom()).monsters = MonsterHelper.getEncounter(THE_HEART_ENC);
                        (AbstractDungeon.getCurrRoom()).rewards.clear();
                        (AbstractDungeon.getCurrRoom()).rewardAllowed = false;
                        (AbstractDungeon.getCurrRoom()).smoked = false;
                        AbstractDungeon.player.isEscaping = false;

                        (AbstractDungeon.getCurrRoom()).phase = AbstractRoom.RoomPhase.COMBAT;
                        (AbstractDungeon.getCurrRoom()).monsters.init();
                        AbstractRoom.waitTimer = 0.1F;
                        AbstractDungeon.player.preBattlePrep();
                        _instance.hasFocus = false;
                        GenericEventDialog.hide();
                        _instance.roomEventText.clear();
                        _instance.roomEventText.hide();
                        CardCrawlGame.fadeIn(1.5F);
                        try {
                            Field f = SpireHeart.class.getDeclaredField("npc");
                            f.setAccessible(true);
                            ((AnimatedNpc)f.get(_instance)).dispose();
                            f.set(_instance, null);
                        } catch (Exception ex) {
                            System.out.println("NPC Dispose ERROR: " + ex.getMessage());
                        }
                        AbstractDungeon.rs = AbstractDungeon.RenderScene.NORMAL;
                        _instance.combatTime = true;

                        AbstractDungeon.lastCombatMetricKey = THE_HEART_ENC;
                        break;
                }
                return SpireReturn.Return();
            }
            return SpireReturn.Continue();
        }
    }
}
